﻿// v 0.1
var timer = 60;
var paystate = 0;

$(document).ready(function () {

    if (paystate == 0) {

        getPayStatus();

    }

    $('#btnpay').click(function () {

        disableButton();

        $('#payframe').height(600);

    });

});

function enableButton() {

    $('#btnpay').show();
    $('#btnpay2').hide();
}

function disableButton() {

    $('#btnpay').hide();
    $('#btnpay2').show();
}

function getPayStatus() {

    if (paystate == 1) return;

    var orderno = $("#lblPayOrderNO").html();
    //alert(orderno);

    if (orderno == '') return;

    var orderhtm = "<a href='../bookingengine/searchresv.aspx?confnum=" + orderno + "' target='_blank'>查看订单详情</a>";

    $.ajax({

        timeout: 30000,

        url: '../pay/payinfo.aspx',
        dataType: '',
        cache: false,
        data: {
            action: 'getPayStatus',
            orderno: orderno
        },
        beforeSend: function () {

            $("#labpayok").html('正在查询支付结果...' + orderhtm);
        },
        success: function (data) {

            //alert(data);

            if (data == '-1') {

                timer = 60;

                $("#labpayok").html("无相关支付信息。");

                TimeOut();
            }
            else if (data == "100") {

                timer = 20;

                $("#labpayok").html("等待支付中," + orderhtm);

                TimeOut();

            }
            else if (data == "0") {

                timer = 5;

                $("#labpayok").html("查询支付接口返回错误!" + orderhtm);

                //enableButton();

                TimeOut();
            }
            else if (data == "1") {

                //alert('支付成功！');

                $("#labpayok").html("支付已成功!" + orderhtm);

                paystate = 1;

                paySuccess();

            }
            else if (data == "2") {
                $("#labpayok").html("您好，你的订单尚未支付." + orderhtm);
            }
            else if (data == '-4') {

                timer = 10;

                $("#labpayok").html("数据保存出错！重试...");

                TimeOut();
            }
            else if (data == "3") {

                timer = 20;

                $("#labpayok").html("订单支付中," + orderhtm);

                //enableButton();

                TimeOut();
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {

            $("#labpayok").html("查询支付接口返回错误!");

            $('#btnpay').attr('disabled', false);
        }
    });
}

function TimeOut() {

    timer = timer - 1;

    if (timer > 0) {
        setTimeout("TimeOut()", 1000);
    }
    else {
        getPayStatus();
    }
}